package com.lagou.servlet;

import com.lagou.entity.PageBean;
import com.lagou.entity.Student;
import com.lagou.service.StudentService;
import com.lagou.service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "StudentPageQueryServlet", urlPatterns = "/studentPageQueryServlet")
public class StudentPageQueryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、获取前端页面传来的页码数值并构造分页查询的对象
        String page = request.getParameter("page");
        System.out.println("前端页面传过来的页码是： "+ page);
        PageBean pageBean = null;
        if(null != page && page.length()>0) {
            //根据前台传递过来的页码进行对象的创建
            pageBean = new PageBean(Integer.parseInt(page), 10);
        }

        //通过Service层以及Dao层获取所有学员的信息
        StudentService studentService =  new StudentServiceImpl();

        //准备集合保存获取到的所有学生信息
        List<Student> studentList = studentService.studentShowService(pageBean);

        //记录总页数
        int maxLen = studentList.size()/10 + 1;

        //将获取到的所有学员信息放到内置对象，通过客户端跳转的方式显示出来
        request.getSession().setAttribute("studentList", studentList);

        //当查询所有学员信息时，将总数量放入session中
        if(null == page) {
            request.getSession().setAttribute("maxLen", maxLen);
            System.out.println("总页数为： "+ maxLen);
        }
        response.sendRedirect("manageStudent.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
